import {
  Controller,
  Post,
  Body,
  Get,
  Req,
  UnauthorizedException,
} from '@nestjs/common';
import { Request } from 'express';
import { AuthGuard } from '@nestjs/passport';
import { AuthService } from './auth.service';
import { CreateUserDto } from '../user/dto/user.dto';

@Controller('auth')
export class AuthController {
  constructor(private authService: AuthService) {}

  // @UseGuards(AuthGuard('local'))
  @Post('login')
  async login(@Body() credentials: { username: string; password: string }) {
    const { username, password } = credentials;
    return this.authService.login({ username, password });
  }

  @Get('userInfo')
  async getUserInfo(@Req() req: Request) {
    const token = req.headers.authorization?.replace('Bearer ', '');
    if (!token) {
      throw new UnauthorizedException('未提供令牌');
    }
    console.log(this.authService.getUserInfo(token), '----token');
    return {
      auths: ['*', 'member', 'lowcode', 'addlowcode'],
    };
  }
}
